<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2021/5/18
 * Time: 18:22
 */

namespace app\service;


use app\model\ArticleModel;

class ArticleService
{
    /**获取列表
     * @param $param
     */
    public function getList($page,$limit=10){
        $articleModel = new ArticleModel();
        $start = $page*$limit;
        $end = $start+$limit;
        $list =   $articleModel->order('id', 'desc')->limit($start,$end)->select();
        return $list;
    }

    /**文章详情
     * @param $id文章id
     */
    public function detail($id){
        $articleModel = new ArticleModel();
        $article  = $articleModel->where('id',$id)->find();
        return $article;
    }

    public function mulQuery($where,$limit,$orderBy='id',$sort='desc'){
        $articleModel = new ArticleModel();
        $list  = $articleModel->where($where)->order($orderBy,$sort)->limit($limit)->select();
        return $list;
    }

    public function randData($limit=5){
        $articleModel = new ArticleModel();
        $list  = $articleModel->orderRand()->limit($limit)->select();
        return $list;
    }

    /**
     * @param $dataArr
     * @return mixed
     */
    public function add($dataArr){
        $articleModel = new ArticleModel();
        #校验文章是否重复
        $where = [
            'title'=>$dataArr['title']
        ];
        $info = $articleModel->field('id')->find( $where);
        if($info){
            return $info['id'];
        }
        $dataArr['created_at'] = date('Y-m-d H:i:s');
        $articleModel->data($dataArr)->save();
        return $articleModel->getLastInsID();
    }
}